//
//  NewspaperViewController.m
//  wanbao
//  读报纸首页
//  Created by 马文涛 on 14-1-17.
//  Copyright (c) 2014年 Mondeo. All rights reserved.
//

#import "NewspaperViewController.h"
#import "NewspaperEditionViewController.h"
#import "MyUIButton.h"

@interface NewspaperViewController ()
{
    UIScrollView *bgSV;
}
@end

@implementation NewspaperViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 顶部标题
    float topHeight = 52;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top"]];
    imageView.frame = CGRectMake(0, 0, deviceWidth, topHeight);
    
    // 底部导航
    float bottomHeight = 50;
    UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, deviceHeight-  bottomHeight, deviceWidth, bottomHeight)];
    bottomView.userInteractionEnabled = YES;
    bottomView.image = [UIImage imageNamed:@"button"];
    
    // 返回按钮
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 15, 24, 17)];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backFun) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:backBtn];
    
    // 刷新
    UIButton *refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(deviceWidth - 40, 15, 24, 17)];
    [refreshBtn setImage:[UIImage imageNamed:@"fresh"] forState:UIControlStateNormal];
    [refreshBtn addTarget:self action:@selector(refrenshFun) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:refreshBtn];
    
    // 主视图
    bgSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, deviceWidth, deviceHeight)];
    
    // 创建报纸按钮
    float bookrackHeight = 209;
    float bookWidth = 110;
    float bookHeight = 150;
    float bookX = (deviceWidth - bookWidth * 2) / 3;
    for (int i = 0; i < 3; i++) {
        UIImageView *bookrackImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, i * bookrackHeight - 7, deviceWidth, bookrackHeight)];
        bookrackImg.userInteractionEnabled = YES;
        bookrackImg.image = [UIImage imageNamed:@"yiceng"];
        if (bgSV.subviews.firstObject) {
            [bgSV insertSubview:bookrackImg belowSubview:bgSV.subviews.firstObject];
        }else{
            [bgSV addSubview:bookrackImg];
        }
        
        
        // iPhone4三个书层，两层报纸
        if (i >= 2) {
            break;
        }
        for (int j = 0; j < 2; j++) {
            MyUIButton *myBtn = [[MyUIButton alloc] initWithFrame:CGRectMake((j+1)*bookX + j*bookWidth , 68, bookWidth, bookHeight)];
            [myBtn setImage:[UIImage imageNamed:@"newspaper"] forState:UIControlStateNormal];
            [myBtn addTarget:self action:@selector(openNewspaperFun:) forControlEvents:UIControlEventTouchUpInside];
            myBtn.tag = myBtn.frame.origin.y;
            [bookrackImg addSubview:myBtn];
            
            // 报纸的日期
            UIView *dateImg = [[UIView alloc] initWithFrame:CGRectMake((bookWidth-90)/2, bookHeight-21, 90, 21)];
            dateImg.layer.cornerRadius = 10;
            [dateImg setBackgroundColor:[UIColor colorWithRed:1 green:0.1 blue:0.1 alpha:0.5]];
            //dateImg.image = [UIImage imageNamed:@"date"];
            [myBtn addSubview:dateImg];
            
            UILabel *datelabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 80, 21)];
            datelabel.font = [UIFont fontWithName:@"Helvetica" size:12];
            datelabel.textAlignment = NSTextAlignmentCenter;
            datelabel.textColor = [UIColor whiteColor];
            datelabel.text = [NSString stringWithFormat:@"2014.0%d.2%d", j+1, j+1];
            [dateImg addSubview:datelabel];
        }
    }
    
    [self.view addSubview:bgSV];
    [self.view addSubview:bottomView];
    [self.view addSubview:imageView];
}

- (void)openNewspaperFun:(MyUIButton*)sender
{
    NewspaperEditionViewController *editoinVC = [[NewspaperEditionViewController alloc] initWithNewspaperFrame:sender.frame];
    [self.navigationController pushViewController:editoinVC animated:YES];
}

- (void)backFun
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refrenshFun
{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    // 隐藏状态栏
    [UIApplication sharedApplication].statusBarHidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
